package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.Coach;
import com.zut.domain.Evaluation;
import com.zut.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    /*查询所有评价*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Evaluation> evaluationPageInfo = evaluationService.getAll(pageNum, pageSize);
        Integer code = evaluationPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = evaluationPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, evaluationPageInfo, msg);
    }

    /*保存评价*/
    @PostMapping
    public Result insert(@RequestBody Evaluation evaluation) {
        boolean flag = evaluationService.insert(evaluation);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*删除评价*/
    @DeleteMapping("/{userId}/{evaluationId}")
    public Result deleteById(@PathVariable String userId, @PathVariable String evaluationId) {
        boolean flag = evaluationService.deleteById(userId, evaluationId);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改评价*/
    @PutMapping
    public Result Update(@RequestBody Evaluation evaluation) {
        boolean flag = evaluationService.update(evaluation);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据教练id查询所有评价*/
    @GetMapping("/{pageNum}/{pageSize}/{id}")
    public Result SelectById(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id) {
        PageInfo<Evaluation> evaluationPageInfo = evaluationService.getById(pageNum, pageSize, id);
        Integer code = evaluationPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = evaluationPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, evaluationPageInfo, msg);
    }

    @GetMapping("/test/{pageNum}/{pageSize}")
    private Result getAvgGrade(@PathVariable Integer pageNum,@PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> evaluationPageInfo = evaluationService.getAvgGrade(pageNum,pageSize);
        Integer code = evaluationPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = evaluationPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, evaluationPageInfo, msg);
    }
}
